#coding: utf-8
import subprocess

def mToInt(int_str):
    try:
        return int(int_str)
    except ValueError:
        return None
         
def exec_command(command, output_as='string'):
    print command
    child = subprocess.Popen([command], shell=True, stdout=subprocess.PIPE) 
    output = child.stdout.read()
    if output_as == 'array':
        return output.split("\n")
    elif output_as == 'string':
        return output.replace("\n", " ")
    return output
    
def send_to_bot(text, repeat=True):
    import socket
    from time import sleep
    for i in range(5):
        try:
            c = socket.socket(socket.AF_UNIX)
            c.connect('/tmp/jbot.socket')
            c.send(text)          # отправляем свои данные
            sleep(1)
            c.close()                         # закрываем соединение
            return True
        except socket.error:
            if repeat:
                sleep(2)
                return send_to_bot(text, False)
    return False
